package com.sz.app.oss.api.logistics.order;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.base.utils.StringUtils;
import com.sz.biz.logistics.ord.service.OrdServiceOrderService;
import com.sz.biz.logistics.ord.dto.OrdServiceOrderDto;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: 服务订单 controller <br>
 * Author: hpeng.wang <br>
 * Date: 2017-03-31 14:28:00
 */
@RestController
@RequestMapping("/api/v1/order/services")
@Api(description = " ", tags = "036-002、订单-服务")
public class OrdServiceOrderController extends BaseController {
    @Autowired
    private OrdServiceOrderService service;

    /**
     * 新增.
     */
    @ApiOperation(value = "新增", notes = "新增服务订单<br>权限：" + Permissions.BIZ_MGT_ORD_ORDER)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_ORD_ORDER)
    public ResultDto save(@RequestBody @ApiParam(value = "要保存的数据", required = true) OrdServiceOrderDto dto) {
        OrdServiceOrderDto entry = new OrdServiceOrderDto();
        dto.setId(null);//保存时忽略id
        EntityUtils.copyPropertiesIgnoreNull(dto, entry);
        String id = service.saveServiceOrderDto(entry);
        ResultDto result = getSaveSuccessDto(MessagesCodes.MODULE_ORDER_PRODUCT);
        result.setData(id);
        return result;
    }

    /**
     * 批量删除
     *
     * @param ids 要删除的id集合
     * @return 操作结果
     */
    @ApiOperation(value = "删除", notes = "根据指定orderNo集合删除数据<br>权限：" + Permissions.BIZ_MGT_ORD_ORDER, httpMethod = "POST", position = 2)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_ORD_ORDER)
    public ResultDto batchDelete(@RequestBody @ApiParam(value = "要删除的serviceOrderNo集合", required = true) List<String> ids) {
        service.batchDeleteByServiceOrderNos(ids);
        ResultDto dto = getDeleteSuccessDto(MessagesCodes.MODULE_ORDER_PRODUCT);
        return dto;
    }

    /**
     * 修改.
     */
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @ApiOperation(value = "修改", notes = "修改服务订单数据<br>权限：" + Permissions.BIZ_MGT_ORD_ORDER)
    @Transactional(propagation = Propagation.REQUIRED)
    @RequiresPermissions(Permissions.BIZ_MGT_ORD_ORDER)
    public ResultDto update(@RequestBody OrdServiceOrderDto serviceOrderDto) {
        service.updateDtoByServiceOrderNo(serviceOrderDto);
        ResultDto result = getUpdateSuccessDto(MessagesCodes.MODULE_ORDER_PRODUCT);
        return result;
    }

    /**
     * 根据serviceOrderNo查询数据
     *
     * @param serviceOrderNo 服务订单的serviceOrderNo
     * @return 查询到的单条服务订单
     */
    @RequestMapping(value = "/{serviceOrderNo}", method = RequestMethod.GET)
    @ApiOperation(value = "查询_serviceOrderNo", notes = "根据serviceOrderNo获取到的服务订单", position = 4)
    public ResultDto findByServiceOrderNo(@ApiParam(value = "服务订单serviceOrderNo", required = true) @PathVariable String serviceOrderNo) {
        OrdServiceOrderDto serviceOrderDto = service.findDtoByServiceOrderNo(serviceOrderNo);
        ResultDto result = new ResultDto();
        result.setData(serviceOrderDto);
        return result;
    }

    /**
     * 根据条件获取数据集合
     *
     * @param q                参数信息
     * @param pageIndex        哪页
     * @param pageSize         分页大小
     * @param serviceOrderNo 订单编号
     * @param sort             排序字段的名称
     * @param asc              true asc false desc
     * @return 查询的数据集合
     */
    @ApiOperation(value = "查询", notes = "根据指定条件获取数据集合", position = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query", defaultValue
                    = "1"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数,max<2000", dataType = "int", paramType = "query",
                    defaultValue = "10"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "serviceOrderNo", value = "订单编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue =
                    "true")
    })

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultDto find(@RequestParam(required = false) String q,
                          @RequestParam(required = false, defaultValue = "1") int pageIndex,
                          @RequestParam(required = false, defaultValue = "10") int pageSize,
                          @RequestParam(required = false) String serviceOrderNo,
                          @RequestParam(required = false) String sort,
                          @RequestParam(required = false, defaultValue = "true") Boolean asc
    ) {
        q = StringUtils.replaceSqlPattern(q);
        ResultDto result = service.findDtoByParam(new QueryParams(pageSize, pageIndex, q, sort, asc).put("serviceOrderNo", serviceOrderNo));
        return result;
    }
}
